package bishi.alibaba;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @Info:
 * @Author: Tangxz
 * @Date: 2020/09/06 17:31
 */
public class _40 {
    public static void main(String[] args) throws IOException {
        BufferedReader readTxt = new BufferedReader(new FileReader(new File("C:\\Users\\Administrator\\Desktop\\面试、算法\\src\\bishi.alibaba\\_40.txt")));
        String textLine = "";

        String str = "";

        while ((textLine = readTxt.readLine()) != null) {
            str += " " + textLine;
        }
        String ww = " 52114";
        String[] numbersArray = str.replace(" ","").split(",");//这个逗号bai还可以是其他的字符
        int[] nums = new int[120000];
        for (int i=0;i<numbersArray.length;i++){
            nums[i] = Integer.parseInt(numbersArray[i]);
        }

        System.out.println(solution(3, new int[]{1,1,2}));
    }

    public static int solution(int n, int[] nums) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int i =0;i<n;i++){
            if (!map.containsKey(nums[i])){
                map.put(nums[i],1);
            }else{
                map.put(nums[i],map.get(nums[i])+1);
            }
        }
        for (Integer key:map.keySet()){
            int i = 1;//当前的幂数
            boolean p = false;//当前key是否有匹配成功的
            while(i<=1000000000&&!p){
                i*=2;
                if (key<i){
                    int oneKey = i - key;
                    if (map.containsKey(oneKey)){
                        if (oneKey == key && map.get(oneKey)>1){
                            p = true;
                        }else if(oneKey != key){
                            p = true;
                        }
                    }
                }
            }
            if (p){
                n-=map.get(key);
                map.put(key,0);
            }
        }
        return n;
    }
}
